/**
 * @file    Expression classname is undefined on line 2, column 15 in Templates/Classes/Class.java.
 * @author  Haseeb Yousaf
 * @see     LICENSE (MIT style license file)
 * @version 1.0
 * @date    Nov 17, 2011 4:41:55 PM
 */
package carrello.impl.boundary.administrator;

import carrello.impl.controller.administrator.UpdateCategoryCTRL;
import carrello.impl.boundary.AbstractUI;
import carrello.model.entities.*;
import carrello.model.entitydao.CategoryDAO;
import java.util.List;
import static carrello.util.EntityManagerUtil.getEntityManager;


public class UpdateCategoryUI extends AbstractUI{
    private String cname;
    private String cdescription;
    private Category category;
    private int cid;
    private List<Attribute> attList;
    private List<Category> catList;

    public String getCdescription() {
        return cdescription;
    }

    public void setCdescription(String cdescription) {
        this.cdescription = cdescription;
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public int getCid() {
        return cid;
    }

    public void setCid(int cid) {
        this.cid = cid;
    }

    public List<Category> getCatList() {
        return new CategoryDAO().getAll();
    }

    public void setCatList(List<Category> catList) {
        this.catList = catList;
    }

    public List<Attribute> getAttList() {
        return new CategoryDAO().getAttributes(cid);
    }

    public void setAttList(List<Attribute> attList) {
        this.attList = attList;
    }
    
    
    @Override
    public String execute()
    {
        getEntityManager().getHibernateSession().clear();
        if ( !getSessionIsLoggedIn()) {
            return LOGIN;
        }
        if (cid == 0) {
            return "browse";
        }
        if (cid != 0 & cname == null ) {
            getAttList();
            return INPUT;
        } else {
            updateCategory(cname, cdescription, cid);
        }
        return SUCCESS;
    }
    
    public static void updateCategory(String name, String desc, int cid)
    {
        UpdateCategoryCTRL updateCat = new UpdateCategoryCTRL(cid);
        updateCat.updateCategory(name, desc);
    } // updateCategory
}
